// pages/indexsearch/indexsearch.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imageUrlFix: app.d.imageUrlFix,
    searchValue: "",
    searchList: [
    ],
    searchHis:[],
    show:false,
    isNoGoods: false,
    pageNum: 1,//页面
    status: 1,//1刷新， 2加载
    queryHeight: wx.getSystemInfoSync().windowHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.querySearchHis();
  },

  /**
   * 查询搜索记录
   */
  querySearchHis: function(e){
    var that = this;
    app.request("WxGoodsController", "querySearchHis", {
      user_id: app.d.userId,
    }, function(data){
      console.log(data);
      if (data.searchHisList != null && data.searchHisList.length > 0){
        that.setData({
          searchHis: data.searchHisList, 
        });
      }
    })
  },
  
  /**
   * 搜索值保存
   */
  searchValueSave:function(e){
   var that = this;
   if (that.data.searchList.length > 0){
     that.setData({
       searchValue: e.detail.value,
     });
   }else{
     that.setData({
       searchValue: e.detail.value,
       show: false,
     });
   }
  
  },

  /**
   * 获取搜索结果
   */
  getSearchList:function(e){
    var that = this;
    if (e.currentTarget.dataset.content != undefined){
      that.setData({
        searchValue: e.currentTarget.dataset.content,
      });
    }
    that.setData({
      pageNum: 1,
      status: 1,
    });
    if(that.data.searchValue != ''){
      that.querySearchList();
    }else {
      //搜索字段为空时
    }
  },

  //搜索商品
  querySearchList: function(){
    wx.showNavigationBarLoading();
    var that = this;
    var pageNum = that.data.pageNum;
    var value = that.data.searchValue;
    if (value != null && value != '') {
      app.request("WxGoodsController", "querySearchList", {
        searchValue: value,
        userId: app.d.userId,
        currentPage: pageNum,
      }, function (data) {
        var total = data.searchList.pagination.total;
        if (that.data.status == 1) {
          that.setData({
            searchList: [],
          })
        }
        var list = that.data.searchList;
        if(data.searchList.list != null && data.searchList.list.length > 0){
          pageNum += 1;
          for (var i = 0; i < data.searchList.list.length; i++) {
            list.push(data.searchList.list[i]);
          } 
        }
        wx.hideNavigationBarLoading();
        that.setData({
          show: true,
          searchList: list,
          pageNum: pageNum,
          loadLength: 5,
        });
        if (list.length == total) {
          that.setData({
            isNoGoods: true,
          });
        } else {
          that.setData({
            isNoGoods: false,
          });
        }
      }, function(data){
        wx.hideNavigationBarLoading();
      });
    }else{
      wx.hideNavigationBarLoading();
    }
  },

  // 上拉加载
  onLoadmore: function () {
    var that = this;
    if (that.data.loadLength > 0) {
      that.setData({
        status: 2,
        loadLength: -5,
      });
      that.querySearchList();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})